package com.dehui.traders.ui.usercenter;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.databinding.ObservableField;

import com.dehui.traders.bean.BaseResponse;
import com.dehui.traders.bean.UserDetail;
import com.dehui.traders.config.Preferences;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.utils.UserHelp;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;
import retrofit2.Call;
import retrofit2.Response;

public class UserCenterViewModel extends BaseViewModel{

    private Activity activity;

    public ObservableField<String> nickNameText = new ObservableField<>();

    public ObservableField<String> phoneText = new ObservableField<>();

    public ObservableField<String> areaText = new ObservableField<>();

    public ObservableField<String> sexText = new ObservableField<>();

    public BindingCommand logoutOnClick = new BindingCommand(()->{
        Preferences.saveImageUrl(null);
        Preferences.saveUserNickName(null);
        Preferences.saveUserId(null);
        Preferences.saveToken(null);
        Preferences.savePhone(null);
        activity.finish();
    });

    public BindingCommand editrNickNameOnClick = new BindingCommand(()->{
        EditTextActivity.start(activity,EditTextActivity.Type_NickName,nickNameText.get());
    });

    public BindingCommand editPhoneOnClick = new BindingCommand(()->{
        EditTextActivity.start(activity,EditTextActivity.Type_Phone,phoneText.get());
    });

    public BindingCommand editSexOnClick = new BindingCommand(()->{
        showSeletSexView();
    });

    private void showSeletSexView() {
        final String[] items = { "男","女"};
        AlertDialog.Builder listDialog = new AlertDialog.Builder(activity);
        listDialog.setTitle("");
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               saveUser(which == 0 ? "男": "女");
               dialog.dismiss();
            }
        });
        listDialog.show();
    }

    public BindingCommand editAreaOnClick = new BindingCommand(()->{
        EditTextActivity.start(activity,EditTextActivity.Type_Area,areaText.get());
    });

    private UserDetail userDetail;


    public UserCenterViewModel(Activity activity) {
        super(activity.getBaseContext());
        this.activity = activity;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    private void saveUser(String sex) {
        Map<String,String> map = new HashMap<>();
        map.put("sex",sex);
        Call<BaseResponse<Object>> call = NetImpl.saveUser(UserHelp.getUserId(), map);
        call.enqueue(new CallListener<BaseResponse<Object>>() {
            @Override
            public void _onResponse(Response<BaseResponse<Object>> response) {
                BaseResponse<Object> body = response.body();
                if (body.status == 200){
                    sexText.set(sex);
                }else{
                    ToastUtils.showShort("设置失败");
                }
            }

            @Override
            public void _onFailure() {
                ToastUtils.showShort("网络异常");
            }
        });
    }


    public void queryUser() {
        Call<BaseResponse<UserDetail>> call = NetImpl.queryUser(UserHelp.getUserId());
        call.enqueue(new CallListener<BaseResponse<UserDetail>>() {
            @Override
            public void _onResponse(Response<BaseResponse<UserDetail>> response) {
                BaseResponse<UserDetail> body = response.body();
                if (body.status == 200){
                    userDetail = body.result;
                    initUser();
                }
            }

            @Override
            public void _onFailure() {

            }
        });
    }

    private void initUser() {
        nickNameText.set(userDetail.getUserName() == null ? "" : userDetail.getUserName());
        phoneText.set(userDetail.getPhone() == null ? "" : userDetail.getPhone());
        areaText.set(userDetail.getAddress() == null ? "" : userDetail.getAddress());
        sexText.set(userDetail.getSex() == null ? "" : userDetail.getSex());
    }

}
